package com.yanqu.road.server.cmd.activity.busyfarming;

import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_BUSY_FARMING_FRIEND_HOME_LIST,desc = "亲家果园")
public class BusyFarmingFriendHomeListCmd extends AbstractCommand {

    int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BusyFarmingProto.BusyFarmingFriendHomeListReqMsg reqMsg = BusyFarmingProto.BusyFarmingFriendHomeListReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BusyFarmingMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(type != 1 && type !=2){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        if(type == 2 && StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        ret = player.getModule(BusyFarmingModule.class).friendHome(type);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BusyFarmingProto.BusyFarmingFriendHomeListRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingFriendHomeListRespMsg.newBuilder();
            respMsg.setType(type);
            player.sendPacket(Protocol.U_BUSY_FARMING_FRIEND_HOME_LIST, respMsg);
        }
    }
}
